﻿namespace PowerSong.UserInterface
    {
    using System;
    using System.Net;
    using System.Text;
    using System.Windows;
    using System.Reflection;

    /// <summary>
    /// Interaction logic for FeedbackWindow.xaml
    /// </summary>
    public partial class FeedbackWindow : Window
        {

        public FeedbackWindow()
            {
            InitializeComponent();
            }

        public static string EncodeText(string text)
            {

            // Define acceptable characters
            var acceptableCharacters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\"£$%^*()_+=-#@.,?<>:;/\\[] \n";

            // Process each character, adding it to the sent text only if it is acceptable
            var result = string.Empty;
            foreach (char c in text)
                {
                if (acceptableCharacters.Contains(c.ToString())) result += c;
                }

            // Return the result
            return result;

            }


        private void OKButton_Click(object sender, RoutedEventArgs e)
            {

            try
                {

                // Ensure a message has been specified
                if (string.IsNullOrEmpty(MessageTextBox.Text))
                    {
                    throw new ApplicationException("Please type the message you would like to send. You don't need to include any other details if you don't want to.");
                    }

                // Sanitise the input
                var name = EncodeText(NameTextBox.Text);
                var email = EncodeText(EmailAddressTextBox.Text);
                var message = EncodeText(MessageTextBox.Text);

                // What version are we running?
                var version = Assembly.GetExecutingAssembly().GetName().Version.ToString();

                // Put the data together into a request object
                var request = HttpWebRequest.Create(Infrastructure.FeedbackUrl);
                request.Method = "POST";
                request.ContentType = "application/x-www-form-urlencoded";
                var postData = "txtName=" + name + "&txtEmail=" + email + "&txtSource=" + "PowerSong " + version + "&txtMessage=" + message;
                byte[] Data = (new ASCIIEncoding()).GetBytes(postData);
                request.ContentLength = Data.Length;

                // Send the data
                var requestStream = request.GetRequestStream();
                requestStream.Write(Data, 0, Data.Length);
                requestStream.Close();

                // Close the form
                MessageBox.Show("Your feedback has been sent. Thank you very much!", "Thank You!", MessageBoxButton.OK, MessageBoxImage.Information);
                this.DialogResult = true;

                }
            catch (Exception ex)
                {
                Utilities.ShowErrorMessage("Error", "Your feedback could not be sent:", ex);
                }

            }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
            {
            this.DialogResult = false;
            }
		
        }

    }
